package com.service.impl;

import com.bean.entity.*;
import com.dao.*;
import com.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * 本方法为了把数字串的地址转化成文字版的地址
 * */
@Service
public class TransAddressServiceImpl implements TransAddressService{
    @Autowired
    private ProviceMapper proviceMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private DistrictMapper districtMapper;
    @Autowired
    private StreetMapper streetMapper;
    @Autowired
    private VillageMapper villageMapper;
    @Override
    public  String transAddress(String address){
        try {
            Integer proviceNum= Integer.valueOf(address.substring(0,2)) ;
//            System.out.println(proviceMapper);
            List<Provice> provice = proviceMapper.select(new Provice(proviceNum));

            String addressInfo;
            if (provice.size()!=0){//如果省有数据则查询市
                Integer cityNum=Integer.valueOf(address.substring(2,4));
                List<City> city = cityMapper.select(new City(cityNum));
                if (city.size()!=0){//市有数据则进一步查询区
                    Integer districtNum=Integer.valueOf(address.substring(4,6));

                    List<District> district = districtMapper.select(new District(districtNum));

                    if (district.size()!=0){//区有数据则进一步查询街道
                        Integer streetNum=Integer.valueOf(address.substring(6,10));

                        List<Street> street = streetMapper.select(new Street(streetNum));
                        if (street.size()!=0){//街道有数据则进一步查询小区
                            Integer villageNum=Integer.valueOf(address.substring(10));

                            List<Village> village = villageMapper.select(new Village(villageNum));
                            if (village.size()!=0){//小区有数据则返回
                                addressInfo=provice.get(0).getProviceName()+city.get(0).getCityName()+
                                        district.get(0).getDistrictName()+street.get(0).getStreetName()+
                                        village.get(0).getVillageName();

//                                System.out.println(addressInfo);
                                return addressInfo;
                            }
                            //小区没有数据则返回省、市、区、街道
                            addressInfo=provice.get(0).getProviceName()+city.get(0).getCityName()+
                                    district.get(0).getDistrictName()+street.get(0).getStreetName();
                            return addressInfo;
                        }
                        //返回省、市、区的名字
                        addressInfo=provice.get(0).getProviceName()+city.get(0).getCityName()+ district.get(0).getDistrictName();
                        return addressInfo;
                    }else {
                        //返回省+市的名字
                        addressInfo=provice.get(0).getProviceName()+city.get(0).getCityName();
                        return addressInfo;
                    }
                }else {
                    //如果市数字为空则返回省名称
                    return provice.get(0).getProviceName();
                }
                //如果地址为空则返回null
            }
            else return null;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
